//
//  CouponViewController.m
//  MyNewProject
//
//  Created by 王春辉 on 2020/1/2.
//  Copyright © 2020年 sun. All rights reserved.
//

#import "CouponViewController.h"
#import "CouponTableViewCell.h"
#import "HandInPaymentVC.h"

#pragma mark -- mark 数据相关
#import "MyCouponModel.h"

@interface CouponViewController ()<UITableViewDelegate, UITableViewDataSource> {
    
}

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSArray <MyCouponDataModel *>* dataArray;
@end
static NSString *const identifer = @"coupon_tableview_cell_identifier";
@implementation CouponViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.gk_navTitle = @"我的优惠券";
    self.view.backgroundColor = SKColorWithHex(0xF7F7F7);
    [self initContentView];
    if (self.couponModels.count > 0) {
        self.dataArray = self.couponModels;
        [self.tableView reloadData];
    }else {
        [self requestData];
    }
}

- (void)initContentView {
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.mas_offset(SKTopBar_Height);
        make.bottom.mas_offset(-SKBottomSafe_Height);
    }];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(requestData)];
}


#pragma mark -- mark 懒加载

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.showsVerticalScrollIndicator = false;
        _tableView.showsHorizontalScrollIndicator = false;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [self.view addSubview:_tableView];
        _tableView.tableHeaderView = [UIView new];
        _tableView.tableFooterView = [UIView new];
        _tableView.estimatedRowHeight = 100;
        _tableView.separatorStyle = UITableViewCellSelectionStyleNone;
        [_tableView registerClass:[CouponTableViewCell class] forCellReuseIdentifier:identifer];
    }
    return _tableView;
}
#pragma mark -- mark UITableViewDelegate UITableViewDataSource

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CouponTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer forIndexPath:indexPath];
    MyCouponDataModel *model = self.dataArray[indexPath.row];
    SKWeakSelf;
    cell.money = [model.money intValue];
    cell.title = [model.type isEqualToString:@"1"] ? @"新人专享优惠券" : @"优惠券";
    cell.limit = [NSString stringWithFormat:@"满%@元可用", model.tiaojian];
    cell.date = model.youxiaoqi;
    cell.des = model.msg ? model.msg : cell.title;
    cell.clickButtonBlock = ^{
        [weakSelf cellButtonClick:indexPath];
    };
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}
- (CGFloat)cellHeightForIndexPath:(NSIndexPath *)indexPath cellContentViewWidth:(CGFloat)width tableView:(UITableView *)tableView {
    return self.view.height * 150.0 / 667;
}

#pragma mark -- mark EVENT

/**
 使用优惠券

 @param indexPath indexPath
 */
- (void)cellButtonClick:(NSIndexPath *)indexPath {
    if (self.chooseCouponResultBlock) {
        MyCouponDataModel *model = self.dataArray[indexPath.row];
        self.chooseCouponResultBlock(model);
        [self.navigationController popViewControllerAnimated:true];
    }else {
        [self pushTo];
    }
}


#pragma mark -- mark 业务逻辑
- (void)pushTo {
    [self.tabBarController setSelectedIndex:0];
    [self.navigationController popViewControllerAnimated:true];
    self.tabBarController.tabBar.hidden = false;
}

#pragma mark -- mark 网络数据
- (void)requestData {
    SKWeakSelf;
    NSDictionary *param = @{@"uid":self.uid,
                            @"loginKey":self.loginKey
    };
    [YQNetworking getWithUrl:@"Shop/meShopCoupon" params:param reloginRefreshBlock:^(id response) {
        
    } successBlock:^(id response) {
        MyCouponModel *model = [MyCouponModel modelWithDictionary:response];
        if ([model isSuccess]) {
            weakSelf.dataArray = model.data;
            [weakSelf.tableView reloadData];
        }else {}
        [weakSelf endRefreshing];
    } failBlock:^(NSError *error) {
        [weakSelf debugShowMessage:error.localizedDescription];
        [weakSelf endRefreshing];
    }];
}

- (void)endRefreshing {
    if ([self.tableView.mj_header isRefreshing]) {
        [self.tableView.mj_header endRefreshing];
    }
}
@end
